package etxt2db.section;

import java.util.Arrays;
import java.util.Map;
import java.util.Set;

import etxt2db.corpus.MyAnnotation;
import etxt2db.api.Document;
import etxt2db.tokenizer.Span;

/**
 * An implementation of SectionSplitter in which documents are split 
 * according to UCompare types as specified in the type mappings configuration file.
 *
 * @author Gonçalo Simões
 * @author Rui Lageira
 */
public class UCompareSectionSplitter implements SectionSplitter {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5791147753986455179L;
	
	private String context;
	
	/**
	 * Constructor receiving the type serving as the splitting context.
	 * 
	 * @param context the String representing the UCompare type
	 */
	public UCompareSectionSplitter(String context) {
		this.context = context;
	}
	
	@Override
	public Span[] split(Document text) {
		
		Map<Span,MyAnnotation> tokens = text.getAnnotations().get(context);
		
		if(tokens==null){
			System.err.println("No annotation for type " + context + "! The behaviour of the component may not be what you expect! Please check your workflow!");
			return new Span[]{};
		}
		
		Set<Span> UCTokenSpans = tokens.keySet();
		
		Span[] spans = new Span[UCTokenSpans.size()];
		
		int i = 0;
		
		for(Span s : UCTokenSpans) {
			spans[i++] = s;
		}
		
		Arrays.sort(spans);
		
		return spans;
	}

}
